1367B - Even Array - CodeForces Solution


greedy math *800

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    js=0
    os=0
    cnt=0
    for i in arr:
        if i%2==0:
            os+=1
        else:
            js+=1
    if n%2==0:
        if(os-js>1 or js-os>1):
            print(-1)
            continue
    else:
        if(os-js!=1):
            print(-1)
            continue
    for i in range(0,n,2):
        if arr[i]%2!=0:
            cnt+=1
    print(cnt)

	 	 	    		  	  			 		 	 	  			

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int n;
    vector<int> veven,vodd;
    for(int i=0;i<t;i++)
    {
        veven.clear();
        vodd.clear();
        cin>>n;
        int a[n];
        for(int j=0;j<n;j++)
        {
            cin>>a[j];
            if(j%2==0&&a[j]%2!=0)
                vodd.emplace_back(a[j]);
            else if(j%2!=0&&a[j]%2==0)
                veven.emplace_back(a[j]);
        }
        if(vodd.size()==veven.size())
            cout<<vodd.size()<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness